﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="global.Constants"%>
<html>
<head>
	<%
		String baseUrl = request.getContextPath();
	%>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
	    //这里的全局是为了修改之后的刷新
	    var customListGrid;
		$(document).ready(function(){
		    var idsvalue='';//下拉复选款传入后台ID的值
		    //角色勾选的变量
		    var checkid;
		    var checktext;
		    //部门的变量
		    var deptid;
		    var depttext;
			// 全局参数
			var baseParams = {limit:<%=Constants.PAGE_SIZE %>,start : 0};
			var EXT_QUERY_TIME_OUT = 1200000;
     
            var isSendButton ;
         
            // 通讯录组数据源
		    var phoneListStore = new Ext.data.JsonStore({
		        url: '${pageContext.request.contextPath}/getAddress.action',
		        root: 'datas',
		        fields: ['id', 'phoneid', 'name', 'phone'],
		        baseParams: baseParams,
		        autoLoad: true
		    });
            
         //下拉复选框
         var stateCombo = new Ext.form.ComboBox({   
         store : phoneListStore,   
         displayField:'name', 
         valueField:'id', 
         mode : 'local',
         id : 'stateList',
         typeAhead: true,  
         anchor:'50%',
         fieldLabel : '通讯录组',
         mode: 'local',   
         tpl:'<tpl for="."><div class="x-combo-list-item"><span><input type="checkbox" {[values.check?"checked":""]} value="{[values.id]}" /></span><span >{name}</span></div></tpl>',   
         triggerAction: 'all',   
         emptyText:'请选择',   
         selectOnFocus:true,   
         onSelect : function(record, index){   
             if(this.fireEvent('beforeselect', this, record, index) != false){   
                 record.set('check',!record.get('check'));   
                var str=[];//页面显示的值   
                 var strvalue=[];//传入后台的值
                 this.store.each(function(rc){ 
                     if(rc.get('check')){   
                         str.push(rc.get('name'));   
                         strvalue.push(rc.get('id'));   
                     }   
                 });   
                 this.setValue(str.join());   
                 this.value=strvalue.join();//赋值
                 idsvalue=strvalue.join();//赋值
                 this.fireEvent('select', this, record, index);   
             }
         }
         /*监听默认初始是否选中
         listeners:{
         expand : function(value){//监听下拉事件
         this.store.each(function(rc){
         if(value.value==rc.get('value')){
         rc.set('check',true);//选中
         }
         });
         }
         }
         */
     });
            
            // 新增传报Form
				var addCustomSendFormPanel = new Ext.FormPanel({
					id: 'addUserFormPanel',
			        labelWidth: 60,
			        buttonAlign: 'center',
			        border: false,
			        bodyStyle: 'padding:10px;text-align:left;background-color:transparent;',
			        url: '',
		            items:[{  
			                xtype:'textarea',
			                name: 'context',
			                fieldLabel: '内容',
			                height:110, 
			                anchor:'98%'
			           },stateCombo
			           ,{// 手机号
			                xtype:'textarea',
			                name: 'tele',
			                fieldLabel: '电话号码',
			                height:70, 
			                anchor:'98%'
			           }],
			        buttons: [{
	                    text:'提交',
	                    handler: function(){
				        	var form = addCustomSendFormPanel.getForm();
				        	if(idsvalue == '' && form.findField('tele').getValue() == ''){
				        	   Ext.Msg.alert('错误', '请选择通讯录或者填写号码!!!');
				        	}else{
							    if(form.isValid()){
							    	// 发送请求
									Anynote.ajaxRequest({
										baseUrl: '<%=baseUrl %>',
									    baseParams: form.getValues(),
										action: '${pageContext.request.contextPath}/custom_addcustom.action?ids='+idsvalue,
										callback: function(jsonResult){
											customListGrid.getStore().reload();
											addCustomSendWindow.close();
										},
										showWaiting: true,
										failureMsg: '保存失败.'
									});
								}
				        	}
	                	}
	                },{
	                    text: '重置',
	                    handler: function(){
	                		addCustomSendFormPanel.getForm().reset();
	                    }
	                }]
			    });
            
            // 传报注册窗口
				    var addCustomSendWindow = new Ext.Window({
					title: '添加传报',
					width: 500,
					height: 290,
					modal: true,
					maximizable: false,
					resizable: false,
					iconCls: 'phone_add',
					layout:'fit',
					plain: true,
					items: [addCustomSendFormPanel]
				}).show();
				
			// 传报数据源
		    var customListStore = new Ext.data.JsonStore({
		        url: '${pageContext.request.contextPath}/custom_findCustomSend.action',
		        root: 'datas',
				timeout : EXT_QUERY_TIME_OUT,
		        totalProperty: 'results',
		        fields: ['id','tele','context','addTime','name'],
		        baseParams: baseParams,
		        autoLoad: false,
				listeners : {  
                load : function(r) {  
                isDetailReady = true;// 查询置换标志为已查过
               }
              } 
		    });
		    
		   	    // 工具栏
		    var customListToolbar = new Ext.Toolbar({
		    	renderTo: 'customListToolBarDiv',
		    	items: [
					new Ext.Button({
					    id: 'customList-add-button',
					    text: '添加',
						iconCls: 'phone_add'
						//disabled:true
					}),
					new Ext.Button({
					    id: 'custom-delete-button',
					    text: '删除',
						iconCls: 'phone_delete'
					}),
					'-','查询： 时间',' 起',
					{  
			               xtype:'datefield',
			                id:'customStartTime',
			                hiddenName: 'startTime',
			                fieldLabel: '开始',
                            format:'Y-m-d',
			                mode : 'local',
			                triggerAction: "all",
			                allowBlank:true,
			                editable: false,
			                width: 130,
							vtype : 'daterange',
			                anchor:'98%',
							endDateField : 'customEndTime'
			           },' 止 ',
			           {  
			                xtype:'datefield',
			                id:'customEndTime',
			                hiddenName: 'endTime',
			                fieldLabel: '开始',
                            format:'Y-m-d',
			                mode : 'local',
			                triggerAction: "all",
			                allowBlank:true,
			                editable: false,
							vtype : 'daterange',
			                width: 130,
			                anchor:'98%',
							startDateField : 'customStartTime'
			           },
					new Ext.Button({
					    id: 'custom-query-button',
					    text: '查询',
						iconCls: 'magnifier'
					})
				]
			});

			// 数据表格
			var sm = new Ext.grid.CheckboxSelectionModel();
			customListGrid = new Ext.grid.GridPanel({
				id: 'customListGrid',
				renderTo: 'customListGridDiv',
				border: false,
				stateful: true,
			    autoScroll: 'auto',
		        store: customListStore,
		        loadMask: true,
		        cm: new Ext.grid.ColumnModel({
		            defaults: {
		                width: 100,
		                sortable: true
		            },
			        columns: [  // id','tele','context','name','addTime'
						sm,
						new Ext.grid.RowNumberer({header:'№',width:50}),
						{id:'id',header: '编号', width: 200, hidden: true, sortable: true, dataIndex: 'id'},
						{id:'tele',header: '电话号码', width: 150, hidden: false, sortable: true, dataIndex: 'tele'},
			            {id:'context',header: '内容', width: 300,hidden: false, sortable: true, dataIndex: 'context'},
			            {id:'addTime',header: '发送时间', width: 200,hidden: false, sortable: true, dataIndex: 'addTime'},
			            {id:'name',header: '发送人', width: 200,hidden: false, sortable: true, dataIndex: 'name'}
			           ]
		        }),
		        sm: sm,
		        columnLines: true,
		        bbar: new Ext.PagingToolbar({
					pageSize: <%=Constants.PAGE_SIZE %>,
					store: customListStore,
					displayInfo: true,
					displayMsg: Anynote.PAGINGTOOLBAR_DISPLAYMSG,
					emptyMsg: Anynote.PAGINGTOOLBAR_EMPTYMSG,
					doLoad: function(start){
						baseParams.start = start;
						this.store.load({params: baseParams});
					}
		        })
		    });
		    
			// 设置Grid高度和宽度
			Anynote.resizeGridTo("customListGrid", 0, 56);
			
			// 添加按钮
			$("#customList-add-button").click(function(){
			idsvalue = '';
			// 通讯录组数据源
		    var phoneListStore = new Ext.data.JsonStore({
		        url: '${pageContext.request.contextPath}/getAddress.action',
		        root: 'datas',
		        fields: ['id', 'phoneid', 'name', 'phone'],
		        baseParams: baseParams,
		        autoLoad: true
		    });
            
         //下拉复选框
         var stateCombo = new Ext.form.ComboBox({   
         store : phoneListStore,   
         displayField:'name', 
         valueField:'id', 
         mode : 'local',
         id : 'stateList',
         typeAhead: true,  
         anchor:'50%',
         fieldLabel : '通讯录组',
         mode: 'local',   
         tpl:'<tpl for="."><div class="x-combo-list-item"><span><input type="checkbox" {[values.check?"checked":""]} value="{[values.id]}" /></span><span >{name}</span></div></tpl>',   
         triggerAction: 'all',   
         emptyText:'请选择',   
         selectOnFocus:true,   
         onSelect : function(record, index){   
             if(this.fireEvent('beforeselect', this, record, index) != false){   
                 record.set('check',!record.get('check'));   
                var str=[];//页面显示的值   
                 var strvalue=[];//传入后台的值
                 this.store.each(function(rc){ 
                     if(rc.get('check')){   
                         str.push(rc.get('name'));   
                         strvalue.push(rc.get('id'));   
                     }   
                 });   
                 this.setValue(str.join());   
                 this.value=strvalue.join();//赋值
                 idsvalue = strvalue.join();//赋值
                 this.fireEvent('select', this, record, index);   
             }
         }
         /*监听默认初始是否选中
         listeners:{
         expand : function(value){//监听下拉事件
         this.store.each(function(rc){
         if(value.value==rc.get('value')){
         rc.set('check',true);//选中
         }
         });
         }
         }
         */
     });
			
				// 新增传报Form
                    var addCustomSendFormPanel = new Ext.FormPanel({
					id: 'addUserFormPanel',
			        labelWidth: 60,
			        buttonAlign: 'center',
			        border: false,
			        bodyStyle: 'padding:10px;text-align:left;background-color:transparent;',
			        url: '',
		            items:[{  
			                xtype:'textarea',
			                name: 'context',
			                fieldLabel: '内容',
			                width:350,
			                height:110 
			                
			           },stateCombo
			           ,{// 手机号
			                xtype:'textarea',
			                name: 'tele',
			                fieldLabel: '电话号码',
			                width:350,
			                height:70 
			           }],
			        buttons: [{
	                    text:'提交',
	                    handler: function(){
				        	var form = addCustomSendFormPanel.getForm();
				        	if(idsvalue == '' && form.findField('tele').getValue() == ''){
				        	   Ext.Msg.alert('错误', '请选择通讯录或者填写号码!!!');
				        	}else{
							    if(form.isValid()){
							    	// 发送请求
									Anynote.ajaxRequest({
										baseUrl: '<%=baseUrl %>',
										baseParams: form.getValues(),
										action: '${pageContext.request.contextPath}/custom_addcustom.action?ids='+idsvalue,
										callback: function(jsonResult){
											customListGrid.getStore().reload();
											addCustomSendWindow.close();
										},
										showWaiting: true,
										failureMsg: '保存失败.'
									});
								}
				        	}
	                	}
	                },{
	                    text: '重置',
	                    handler: function(){
	                		addCustomSendFormPanel.getForm().reset();
	                    }
	                }]
			    });
				// 传报注册窗口
				var addCustomSendWindow = new Ext.Window({
					title: '添加传报',
					width: 500,
					height: 290,
					modal: true,
					maximizable: false,
					resizable: false,
					iconCls: 'phone_add',
					layout:'fit',
					plain: true,
					items: [addCustomSendFormPanel]
				}).show();
			});

			 
 
                $("#custom-query-button").click(function(){
			   var startTime = Ext.getCmp("customStartTime").getValue();
			   var endTime = Ext.getCmp("customEndTime").getValue();
			     var basePar  = {limit:<%=Constants.PAGE_SIZE %>,start : 0};
			       basePar.startTime = startTime;
			       basePar.endTime = endTime;
			    customListStore.reload({params:basePar});
			 })
 
 
                // 删除按钮
		 	$("#custom-delete-button").click(function(){
				// 提交
				submit("delete");
			});
			
			
			// 提交
			function submit(type){
				var msg = "是否删除";
				 
				var records = customListGrid.getSelectionModel().getSelections();
				
				if(records.length < 1){
					 Ext.Msg.alert("提示", "请至少选择一条数据.");
				}else{
					   //如果点击的是删除传报
					    
					      Ext.Msg.confirm("警告", msg, function(btn){
						  
							var customIds = "";
							for(var i=0;i<records.length;i++){
								customIds = customIds + records[i].get("id") + ",";
							}
							// 发送请求
							
							Anynote.ajaxRequest({
								baseUrl: '<%=baseUrl %>',
							       baseParams: {customIds:customIds},
								action: '${pageContext.request.contextPath}/custom_deleteCustom.action',
								callback: function(jsonResult){
									customListGrid.getStore().reload();
								},
								showWaiting: true,
								failureMsg: '保存失败.'
							})
						  })
					}
					}
});

	</script>
</head>
<body>
<div id="customListDiv">
	<div id="customListToolBarDiv"></div>
	<div id="customListGridDiv" style="width:100%;height:100%;"></div>
</div>

</body>
</html>
